package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

/**
 * @description: 获取用户信息
 * @author: Hupengpeng
 * @time: 2021/7/3 11:34
 */
public class OauthUserinfo {
    private String open_id;
    private String access_token;
    private String response;

    private OauthUserinfo() {
    }

    protected static OauthUserinfo New() {
        return new OauthUserinfo();
    }

    public OauthUserinfo execute() {
        String url = StrUtil.format("{}{}?open_id={}&access_token={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_OAUTH_USERINFO,
                getOpen_id(),
                getAccess_token());
        String rs = HttpUtil.get(url);
        response = rs;
        return this;
    }

    public OauthUserinfoResponse convert() {
        JSONObject obj = JSONUtil.parseObj(response);
        if ("success".equalsIgnoreCase(obj.getStr("message"))) {
            return JSONUtil.toBean(obj.getJSONObject("data"), OauthUserinfoResponse.class);
        }
        throw new DouYinException(response);
    }

    public String getOpen_id() {
        return open_id;
    }

    public OauthUserinfo setOpen_id(String open_id) {
        this.open_id = open_id;
        return this;
    }

    public String getAccess_token() {
        return access_token;
    }

    public OauthUserinfo setAccess_token(String access_token) {
        this.access_token = access_token;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public OauthUserinfo setResponse(String response) {
        this.response = response;
        return this;
    }

    public class OauthUserinfoResponse {
        private String avatar;
        private String avatar_larger;
        private String captcha;
        private String city;
        private String client_key;
        private String country;
        private String desc_url;
        private String description;
        private String district;
        private String e_account_role;
        private String encrypt_mobile;
        private int error_code;
        private int gender;
        private String nickname;
        private String open_id;
        private String province;
        private String union_id;

        public String getAvatar() {
            return avatar;
        }

        public OauthUserinfoResponse setAvatar(String avatar) {
            this.avatar = avatar;
            return this;
        }

        public String getAvatar_larger() {
            return avatar_larger;
        }

        public OauthUserinfoResponse setAvatar_larger(String avatar_larger) {
            this.avatar_larger = avatar_larger;
            return this;
        }

        public String getCaptcha() {
            return captcha;
        }

        public OauthUserinfoResponse setCaptcha(String captcha) {
            this.captcha = captcha;
            return this;
        }

        public String getCity() {
            return city;
        }

        public OauthUserinfoResponse setCity(String city) {
            this.city = city;
            return this;
        }

        public String getClient_key() {
            return client_key;
        }

        public OauthUserinfoResponse setClient_key(String client_key) {
            this.client_key = client_key;
            return this;
        }

        public String getCountry() {
            return country;
        }

        public OauthUserinfoResponse setCountry(String country) {
            this.country = country;
            return this;
        }

        public String getDesc_url() {
            return desc_url;
        }

        public OauthUserinfoResponse setDesc_url(String desc_url) {
            this.desc_url = desc_url;
            return this;
        }

        public String getDescription() {
            return description;
        }

        public OauthUserinfoResponse setDescription(String description) {
            this.description = description;
            return this;
        }

        public String getDistrict() {
            return district;
        }

        public OauthUserinfoResponse setDistrict(String district) {
            this.district = district;
            return this;
        }

        public String getE_account_role() {
            return e_account_role;
        }

        public OauthUserinfoResponse setE_account_role(String e_account_role) {
            this.e_account_role = e_account_role;
            return this;
        }

        public String getEncrypt_mobile() {
            return encrypt_mobile;
        }

        public OauthUserinfoResponse setEncrypt_mobile(String encrypt_mobile) {
            this.encrypt_mobile = encrypt_mobile;
            return this;
        }

        public int getError_code() {
            return error_code;
        }

        public OauthUserinfoResponse setError_code(int error_code) {
            this.error_code = error_code;
            return this;
        }

        public int getGender() {
            return gender;
        }

        public OauthUserinfoResponse setGender(int gender) {
            this.gender = gender;
            return this;
        }

        public String getNickname() {
            return nickname;
        }

        public OauthUserinfoResponse setNickname(String nickname) {
            this.nickname = nickname;
            return this;
        }

        public String getOpen_id() {
            return open_id;
        }

        public OauthUserinfoResponse setOpen_id(String open_id) {
            this.open_id = open_id;
            return this;
        }

        public String getProvince() {
            return province;
        }

        public OauthUserinfoResponse setProvince(String province) {
            this.province = province;
            return this;
        }

        public String getUnion_id() {
            return union_id;
        }

        public OauthUserinfoResponse setUnion_id(String union_id) {
            this.union_id = union_id;
            return this;
        }

    }
}
